package com.example.ch4;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DownloadBMP
 */
public class DownloadBMP extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DownloadBMP() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/jpeg");
		ServletContext ctx = getServletContext();
		InputStream is = ctx.getResourceAsStream("/ch4/ToDown.jpg");
		int read = 0;
		byte[] bytes = new byte[512];
		OutputStream os = response.getOutputStream();
		while((read = is.read(bytes)) != -1) {
			os.write(bytes);
		}
		os.flush();
		os.close();
	}

}
